using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using SchoolPointe.RelatedContent.Enums;
using SchoolPointe.RelatedContent.Models;
using Orchard.Data;
using SchoolPointe.RelatedContent.Services;
using SchoolPointe.RelatedContent.Extensions;

namespace SchoolPointe.RelatedContent {
    public class Migrations : DataMigrationImpl {
        private readonly IRelatedContentService _relatedContentService;

        public Migrations(IRelatedContentService relatedContentService)
        {
            _relatedContentService = relatedContentService;
        }

        public int Create() {
			// Creating table RelatedContentPartRecord
			SchemaBuilder.CreateTable("RelatedContentPartRecord", table => table
				.ContentPartRecord()
			);

            SchemaBuilder.CreateTable("RelatedContentItemPartRecord", table => table
                    .Column<int>("Id", column => column.PrimaryKey().Identity())
                    .Column<string>("RelationshipIdentifier")
                    .Column<int>("RelatedItem_id")
                    .Column<string>("RelatedItemIdentifier")
                    .Column<int>("RelatedContentPartRecord_Id")
                );

            ContentDefinitionManager.AlterPartDefinition("RelatedContentPart", builder => builder.Attachable());            

            return 2;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.AlterTable("RelatedContentItemPartRecord",
                                     table => table.AddColumn<string>("RelatedItemIdentifier"));
            return 2;
        }
    }
}